<template>
	<view class="user">
		<view class="no-list" v-if="is_init">

		</view>
		<view class="no-list" v-else-if="!is_login " @click="toLogin()">
			- 去登录 -
		</view>
		<view class="no-list" v-else-if="!has_authority ">
			- 没有权限 -
		</view>
		<view class="no-list" v-else-if="!is_open ">
			- 已关店 -
		</view>
		<view class="order sever" v-else-if="is_store">
			<view class="title">
				<text class="h3">商家模块</text>
			</view>
			<view class="tips">
				<view class="tips-item" @click="toPages(item.url,item.event)" v-for="(item,index) in menu" :key="index">
					<image :src="item.img" mode=""></image>
					<text>{{item.name}}</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/material/index')">
					<image src="/static/image/faxian.png" mode=""></image>
					<text>素材中心</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/user/chat/index')">
					<image src="/static/index/s7.png" mode=""></image>
					<text>辅助聊天</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/user/role')">
					<image src="/static/index/s7.png" mode=""></image>
					<text>切换身份</text>
				</view>
				<!-- <view class="tips-item" @click="toPages('/shoper/user/room/index')">
					<image src="/static/index/room.png" mode=""></image>
					<text>房间列表</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/staff/index')">
					<image src="/static/index/yg.png" mode=""></image>
					<text>员工列表</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/sign/staffsign')">
					<image src="/static/index/yg.png" mode=""></image>
					<text>签到记录</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/group/index')">
					<image src="/static/index/yg.png" mode=""></image>
					<text>组列表</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/penalty/index')">
					<image src="/static/index/pd.png" mode=""></image>
					<text>收费管理</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/setting/index')">
					<image src="/static/index/cs.png" mode=""></image>
					<text>参数设定</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/charge/index')">
					<image src="/static/index/fw.png" mode=""></image>
					<text>服务费</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/fine/index')">
					<image src="/static/index/fk.png" mode=""></image>
					<text>我的罚款</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/goods/list')">
					<image src="/static/index/sp.png" mode=""></image>
					<text>商品列表</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/order/index')">
					<image src="/static/index/dd.png" mode=""></image>
					<text>订单列表</text>
				</view> -->
				<!-- <view class="tips-item" @click="toPages('/shoper/user/chat/index')">
					<image src="/static/index/s8.png" mode=""></image>
					<text class="unread" v-show="unread > 0">{{unread}}</text>
					<text>交流平台</text>
				</view> -->
			</view>
		</view>
		<view class="order sever" v-else-if="is_staff">
			<view class="title">
				<text class="h3">员工模块</text>
				<!-- <text class="all" >全部></text> -->
			</view>
			<view class="tips">
				<view class="tips-item" @click="toPages('/shoper/princess/room/index')">
					<image src="/static/index/room.png" mode=""></image>
					<text>房间列表</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/sign/index')" v-if="show_sign">
					<image src="/static/index/yg.png" mode=""></image>
					<text>签到</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/sign/staffsign?type=leader')"
					v-if="staff && staff.role.value == 1">
					<image src="/static/index/yg.png" mode=""></image>
					<text>店员签到</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/user/sign/staffsign?type=group')"
					v-if="staff.group_leader == 1">
					<image src="/static/index/yg.png" mode=""></image>
					<text>组员签到</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/supervision/index')">
					<image src="/static/index/pd.png" mode=""></image>
					<text>监督部</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/guests/index')">
					<image src="/static/index/cs.png" mode=""></image>
					<text>客资</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/physiology/index')">
					<image src="/static/index/fw.png" mode=""></image>
					<text>生理期设定</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/fine/index')">
					<image src="/static/index/fk.png" mode=""></image>
					<text>我的罚款</text>
				</view>
				<!-- <view class="tips-item" @click="toPages('/shoper/princess/ticket/index')">
					<image src="/static/image/bianji.png" mode=""></image>
					<text>我的台票</text>
				</view> -->
				<view class="tips-item" @click="toPages('/shoper/princess/room/my')">
					<image src="/static/image/bianji.png" mode=""></image>
					<text>我的房间</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/user/chat/index')">
					<image src="/static/index/s7.png" mode=""></image>
					<text>辅助聊天</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/material/index')">
					<image src="/static/index/s7.png" mode=""></image>
					<text>素材中心</text>
				</view>
				<view class="tips-item" @click="toPages('/shoper/princess/customer/index')">
					<image src="/static/image/kefu.png" mode=""></image>
					<text>客讯</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/user/role')">
					<image src="/static/index/s7.png" mode=""></image>
					<text>切换身份</text>
				</view>
			</view>
		</view>
		<view class="no-list" v-else>
			<image src="/static/image/wqx.png" mode="widthFix"></image>
		</view>
		<!-- 月票 -->
		<view class="yp" v-if="show_yp">
			<view class="bg">
				<view class="box">
					<view class="title">

						{{errinfo.title}}
					</view>
					<view class="content">

						{{errinfo.content}}
					</view>
					<button @click="toPay" :loading="loading" :disabled="loading">马上去续费</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		UserInfo,
		mpLogin,
		getMpUserOpenid
	} from "@/api/api.js"
	import {
		getStaff,
		payVip
	} from "@/api/staff.js"
	import {
		checkUserIdentity,
		closeStore
	} from "@/api/user.js"
	import uniCopy from '@/components/js_sdk/xb-copy/uni-copy.js'
	export default {
		data() {
			return {
				is_init: true,
				show_yp: false,
				has_authority: false,
				info: [],
				isshow: false,
				is_login: false,
				open_id: '',
				is_open: true,
				is_staff: false,
				is_store: false,
				unread: 0,
				room_id: null,
				is_connect: false,
				store_id: '',
				websocket_url: '',
				loading: false,
				menu: [],
				show_sign: false,
				staff: '',
				errinfo: {
					title: '抱歉，您的会员服务已经到期',
					content: '服务于2024-05-20 12:00:00到期,请及时续费以启用系统!'
				}
			}
		},
		onLoad() {

		},
		onShow() {
			this.UserInfo()
		},
		onUnload() {
			uni.closeSocket({

			})
		},
		methods: {
			toLogin() {
				// #ifdef H5
				uni.navigateTo({
					url: '/pages/login/index'
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.navigateTo({
					url: '/pages/login/login'
				})
				// #endif
			},
			toPay() {
				var that = this
				that.loading = true
				payVip().then(res => {
					if (res.code == 0) {
						that.show_yp = false
						if (res.data.unifiedorder) {
							let paymentData = res.data.unifiedorder
							uni.requestPayment({
								timeStamp: paymentData.timeStamp,
								nonceStr: paymentData.nonceStr,
								package: "prepay_id=" + paymentData.prepay_id,
								signType: 'MD5',
								paySign: paymentData.paySign,
								success: function(result) {
									uni.$showMsg(result.message)
									that.loading = false
									that.UserInfo()
								},
								fail: function(err) {
									that.loading = false
								}
							})
						} else {
							uni.$showMsg(res.msg)

						}
					} else {
						that.loading = false
						uni.$showMsg(res.msg)
					}
				})
			},

			async UserInfo() {
				const pages = getCurrentPages()
				const currentPage = pages[pages.length - 1]
				await UserInfo().then(res => {
					this.is_init = false
					if (res.code == 0) {
						if (res.data.user) {
							// this.menu = res.data.menu
							this.info = res.data.user
							this.is_login = true
							// this.is_open = res.data.is_open
							// this.is_staff = res.data.is_staff
							// this.is_store = res.data.is_store
							this.unread = res.data.unread
							this.room_id = res.data.room_id
							this.store_id = res.data.store_id
							this.websocket_url = res.data.websocket
							if (this.socketTask) {
								uni.closeSocket({

								})
							}
							// if (this.is_staff) {
							// 	this.getStaff()
							// }
							// if (this.is_open) {
							// 	this.webSocket()
							// }
						} else {
							this.is_staff = false
							this.is_store = false
							this.is_login = false
							this.unread = 0
							this.room_id = null
							this.info = null
							this.$store.dispatch('clearState')
						}
					}
				})
				if (this.is_login) {
					await checkUserIdentity().then(res => {
						if (res.code == 0) {
							let response = res.data
							if (!response.is_visitor && this.$store.state.identity == '') {
								uni.redirectTo({
									url: '/pages/user/role?redirect=' + encodeURIComponent(currentPage
										.route)
								})
								return false
							}
							this.has_authority = !response.is_visitor //游客没有权限
							this.is_store = response.is_store
							this.is_staff = response.is_staff
							this.is_open = response.is_open
							if (this.is_store) {
								this.menu = response.menu
								this.$store.state.permission = response.permission
							}
							if (this.is_staff) {
								this.show_sign = response.show_sign
								this.getStaff()
							}
							if (this.is_open) {
								this.webSocket()
							}
						} else if (res.code == 501) {
							uni.redirectTo({
								url: '/pages/user/role?redirect=' + encodeURIComponent(currentPage
									.route)
							})
							return false
						} else {
							uni.$showMsg(res.msg)
							this.is_staff = false
							this.is_store = false
							this.unread = 0
							this.room_id = null
							this.info = null
							this.$store.dispatch('clearState')
						}
					})
				}
			},
			getStaff() {
				getStaff().then(res => {
					if (res.code == 500) {
						this.show_yp = true
						this.errinfo = JSON.parse(res.msg)
					} else if (res.code == 0) {
						this.staff = res.data
					} else {
						this.is_staff = false
						this.$store.dispatch('clearState')
						uni.$showMsg(res.msg)
					}
				})
			},
			webSocket() {
				var that = this
				// uni.showLoading({
				// 	title: '连接房间。。。'
				// })
				that.socketTask = uni.connectSocket({
					// url: 'wss://www.example.com/socket',
					url: `${this.websocket_url}?user_id=${this.info.id}`,
					complete: () => {

					}
				});
				that.socketTask.onOpen(function(res) {
					// uni.hideLoading()
					console.log('WebSocket连接已打开！', that.socketTask);
					that.is_connect = true
				});
				that.socketTask.onError(function(res) {
					that.is_connect = false
					console.log('WebSocket连接打开失败，请检查！');
					// uni.showLoading({
					// 	title: '重新连接。。。'
					// })
					that.webSocket()
				});
				that.socketTask.onClose(function(res) {
					console.log('WebSocket 已关闭！');
				});
				that.socketTask.onMessage(function(res) {
					let data = JSON.parse(res.data)
					console.log('收到服务器内容：', data, that.is_connect);
					if (data.type == 'ping') {
						return
					}
					if (data.code == 0) {
						let content = data.content
						if (data.type == 'tips') {
							that.unread++
							uni.vibrate()
						}
					}

				});
			},

			toPages(url, event = '') {
				let that = this
				if (!this.is_login) {
					uni.$showMsg('请先登录')
					// #ifdef H5
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/index'
						})
					}, 500)
					// #endif
					// #ifdef MP-WEIXIN
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 500)
					// #endif
				} else {
					if (url != '') {
						uni.navigateTo({
							url: url
						})
					} else {
						if (event == 'close') {
							uni.showModal({
								title: '关店提示',
								content: '确定要关店吗',
								success(data) {
									if (data.confirm) {
										closeStore().then(res => {
											if (res.code == 0) {
												that.UserInfo()
											} else {
												uni.$showMsg(res.msg)
											}
										})
									}
								}
							})
						}
					}

				}

			}
		}
	}
</script>

<style lang="scss">
	.user {
		background: transparent;
	}

	.yp {
		background: #00000080;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		width: 100vw;
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;

		.bg {
			background: #fff;
			width: 90%;
			padding: 30px 15px;
			box-sizing: border-box;
			border-radius: 8px;
			text-align: center;

			.title {
				font-size: 16px;
				font-weight: bold;
			}

			.content {
				width: 80%;
				margin: 20px auto;
				color: #777;
			}

			button {
				background: $uni-bg-color;
				font-size: 14px;
				color: #fff;
				width: 80%;
			}
		}
	}

	.top {
		padding-top: 10px;
		display: flex;
		padding-left: 5%;
		background: $uni-bg-color;
		padding-bottom: 20px;

		.header_logo {
			height: 60px;
			width: 60px;
			border-radius: 50%;
		}

		.userinfo {
			color: #fff;
			flex: 1;
			margin-left: 3%;
			// background: url(@/static/index/tiaowen.png);
			background-size: 100% 100%;

			.title {
				display: flex;
				align-items: center;
				background: transparent;
				font-size: 14px;
				color: #fff;
				margin: 0;
				padding: 0;
				text-align: left;

				image {
					width: 20px;
					height: 20px;
				}

				.name {
					display: block;
					width: 50%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.level {
					position: relative;
					background: linear-gradient(90deg, #FFEBC0, #FFC479);
					color: #6C2A09;
					border-radius: 20px;
					padding: 0 5%;
					font-size: 12px;
					line-height: 20px;
					margin-left: 20%;

					image {
						position: absolute;
						left: -18rpx;
						top: 0;
					}
				}

				.share {
					margin-left: 15%;
				}
			}

			.phone {
				font-size: 24rpx;
				margin: 30rpx 0;
			}

			.code {
				display: flex;
				align-items: center;

				.code-text {
					font-size: 13px;
					max-width: 55%;
					display: block;
				}

				.copy {
					background: #F1D3A8;
					color: #6C2A09;
					padding: 1% 3%;
					border-radius: 5px;
					font-size: 13px;
					margin-left: 5%;
				}

				image {
					width: 16px;
					height: 13px;
					margin-left: 10%;
				}
			}
		}

	}

	.wallet {
		background: url(/static/index/card.png);
		background-size: 100% 100%;
		width: 96%;
		margin: 0 auto;
		margin-top: -40px;
		color: #fff;
		height: 165px;
		position: relative;

		.tixian {
			padding-top: 30px;
			padding-left: 5%;

			text {
				font-size: 22px;
			}
		}

		.tixian_now {
			position: absolute;
			right: 5%;
			top: 15%;
			border-radius: 20px;
			background: #ffffffb3;
			color: #C6A06F;
			padding: 1% 4%;
			font-size: 14px;
		}

		.wallet_info {
			display: flex;
			justify-content: space-around;
			text-align: center;
			margin-top: 35px;

			view {
				flex: 1;
				display: flex;
				flex-direction: column;
				border-right: 1px solid #fff;

				&:last-child {
					border: 0;
				}

				.num {
					font-weight: bold;
					font-size: 16px;
					margin-bottom: 5px;
				}
			}
		}
	}

	.order {
		padding: 0 5%;
		margin-top: 30rpx;
		background: #fff;
		padding-bottom: 40rpx;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 5% 0;
			border-bottom: 1px solid #efefef;

			.h3 {
				font-weight: bold;
				color: #000;
				font-size: 15px;
			}

			.all {
				color: #999999;
				font-size: 14px;
			}
		}

		.tips {
			display: flex;
			justify-content: space-around;
			margin-top: 30rpx;

			.tips-item {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				position: relative;

				image {
					width: 28px;
					height: 28px;
				}

				.unread {
					position: absolute;
					top: -10px;
					right: 20px;
				}

				text {
					font-size: 14px;
					margin-top: 10rpx;
					color: #777777;
				}
			}
		}
	}

	.sever {
		.tips {
			display: table;
			width: 100%;
		}

		.tips-item {
			float: left;
			width: 25%;
			margin-bottom: 30rpx;
		}
	}
</style>